package com.nip.trainning.DataTransferObject;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.nip.trainning.Common.Constant;

public class PersonDataTable extends SQLiteOpenHelper implements DataBaseBase {

	public PersonDataTable(Context context, String pathFile) {

		super(context, pathFile + "//" + Constant.DATA_BASE_NAME, null,
				Constant.DATA_BASE_VERSION);
		SQLiteDatabase.openOrCreateDatabase(pathFile + "//"
				+ Constant.DATA_BASE_NAME, null);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String query = "CREATE TABLE " + Constant.PERSON_INFOMATION_TABLE_NAME
				+ "(" + Constant.PERSON_ID + " INTEGER PRIMARY KEY,"
				+ Constant.PERSON_NAME + " TEXT NOT NULL,"
				+ Constant.PHONE_NUMBER + " TEXT NOT NULL )";

		db.execSQL(query);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("drop table if exists "
				+ Constant.PERSON_INFOMATION_TABLE_NAME);

		// Create tables again
		onCreate(db);
	}

	@Override
	public void Delete() {
		// TODO Auto-generated method stub

	}

	@Override
	public void Insert() {
		// TODO Auto-generated method stub

	}

	@Override
	public void Update() {
		// TODO Auto-generated method stub

	}

	/**
	 * Regist the people into database
	 * 
	 * @param person
	 */
	public void InsertNewPerson(PersonEntity person) {
		SQLiteDatabase mdb = getWritableDatabase();
		ContentValues ctValue = new ContentValues();
		ctValue.put(Constant.PERSON_NAME, person.getPersonName());
		ctValue.put(Constant.PHONE_NUMBER, person.getPhoneNumber());

		mdb.insert(Constant.PERSON_INFOMATION_TABLE_NAME, null, ctValue);
		mdb.close();
	}

	public List<PersonEntity> GetAllPersonInfomation() {
		SQLiteDatabase sqldt = getReadableDatabase();
		String sql = "SELECT * FROM " + Constant.PERSON_INFOMATION_TABLE_NAME;

		Cursor cursor = sqldt.rawQuery(sql, null);

		if (cursor.moveToFirst()) {
			List<PersonEntity> listPeople = new ArrayList<PersonEntity>();
			PersonEntity people;
			do {
				people = new PersonEntity();
				people.setPersonID(cursor.getInt(cursor
						.getColumnIndex(Constant.PERSON_ID)));
				people.setPersonName(cursor.getString(cursor
						.getColumnIndex(Constant.PERSON_NAME)));
				people.setPhoneNumber(cursor.getString(cursor
						.getColumnIndex(Constant.PHONE_NUMBER)));
				listPeople.add(people);

			} while (cursor.moveToNext());
			sqldt.close();
			return listPeople;
		}
		return null;

	}

	/**
	 * Update the people
	 * 
	 * @param person
	 */
	public void UpdatePeople(PersonEntity person) {
		SQLiteDatabase mdb = getWritableDatabase();
		ContentValues ctValue = new ContentValues();
		ctValue.put(Constant.PERSON_NAME, person.getPersonName());
		ctValue.put(Constant.PHONE_NUMBER, person.getPhoneNumber());

		mdb.update(Constant.PERSON_INFOMATION_TABLE_NAME, ctValue,
				Constant.PERSON_ID + "=?",
				new String[] { String.valueOf(person.getPersonID()) });
		mdb.close();
	}

	/**
	 * Delete people by id
	 * @param id
	 */
	public void DeletePeople(int id) {
		SQLiteDatabase mdb = getWritableDatabase();
		
		mdb.delete(Constant.PERSON_INFOMATION_TABLE_NAME, Constant.PERSON_ID
				+ "=?", new String[] { String.valueOf(id) });
		
		mdb.close();
	}
}
